<?php

/***************************************************************\
* FILE:		sys/pages/mailbox.inc
* SECTION:	pages > mails
* FUNCTION:	give an overview of the contents of a mailbox
* USES:	
* INCLUDES:	sys/mailfunctions.inc	
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	check_access("READ");
	if (!$_SESSION['user_id'])
		errorpage(6);
}

/**
 *
 */
function page_inc_execute_action($action) {
	global $PAGE;

	switch($action) {
		default:
			add_alert("unknown action: $action");
			break;
	}
}

/**
 * execute page logic
 */
function page_inc_execute() {
	
	global $PAGE, $CONFIG;
	$CONTENT = array();
	$THISDIR = $PAGE['directory_tree'][$PAGE['dir_depth']-1];
	
	// take care of mailform
	add_action('mail', 'show', 'mf');
	if ($_GET['mailto'])
		$PAGE['mailform'] = mail_form(array('to' => $_GET['mailto']));
	
	// take care of mails in progress
	if ($_POST['submitted'] and $_POST['mailing']) {
		$mail = $_POST;
		$mail['from'] = $_SESSION['user_id'];
		mail_send($mail);
	}
	
	// mailbox browsing actions
	if (!$_GET['out'] and !$_GET['trash']) { // inbox
		$CONTENT['title'] = "INBOX: {$_SESSION['member']['nick']}";
		add_action('outbox', 'url', '.mailbox?out=1');
		add_action('inbox-t', 'url', '.mailbox?trash=1');
		add_action('outbox-t', 'url', '.mailbox?out=1&trash=1');
	} else if (!$_GET['out'] and $_GET['trash']) { // inbox > trash
		$CONTENT['title'] = "INBOX [prullenbak]: {$_SESSION['member']['nick']}";
		add_action('inbox', 'url', '.mailbox');
		add_action('outbox', 'url', '.mailbox?out=1');
		add_action('outbox-t', 'url', '.mailbox?out=1&trash=1');
	} else if ($_GET['out'] and !$_GET['trash']) { // outbox
		$CONTENT['title'] = "OUTBOX: {$_SESSION['member']['nick']}";
		add_action('inbox', 'url', '.mailbox');
		add_action('inbox-t', 'url', '.mailbox?trash=1');
		add_action('outbox-t', 'url', '.mailbox?out=1&trash=1');
	} else if ($_GET['out'] and $_GET['trash']) { // outbox > trashed
		$CONTENT['title'] = "OUTBOX [prullenbak]: {$_SESSION['member']['nick']}";
		add_action('inbox', 'url', '.mailbox');
		add_action('inbox-t', 'url', '.mailbox?trash=1');
		add_action('outbox', 'url', '.mailbox?out=1');
	}
	
	// take care of actions
	if ($_POST['action'] and $_POST['target']) {
	
		if (!ereg("^([0-9]+,)*[0-9]+$", $_POST['target']))
			errorpage(9);
		
		// check access
		if (!$_GET['out']) // inbox
			$sql = db_query_select(array(array(), "MAIL_HEADER.mail_id"), array("MAIL_HEADER INNER JOIN MAIL_TO ON MAIL_HEADER.mail_id = MAIL_TO.mail_id", "DUMMY"), "MAIL_TO.user_id = {$_SESSION['user_id']} AND !MAIL_TO.deleted AND MAIL_HEADER.mail_id IN ({$_POST['target']})");
		else 
			$sql = db_query_select(array(array("mail_id")), array("MAIL_HEADER"), "MAIL_HEADER.sender = {$_SESSION['user_id']} AND !MAIL_HEADER.deleted AND MAIL_HEADER.mail_id IN ({$_POST['target']})");

		if (db_num_rows(db_do_query($sql)) != (substr_count($_POST['target'], ",")+1))
			errorpage(10);
					
		
		// process action
		switch($_POST['action']) {
			case "delete":
				if (!$_GET['out']) // inbox
					db_do_query(db_query_update(array('deleted'), array(1), "MAIL_TO", "mail_id IN ({$_POST['target']})"));	
				else // outbox
					db_do_query(db_query_update(array('sent', 'deleted'), array('sent', 1), "MAIL_HEADER", "mail_id IN ({$_POST['target']})"));
				break;
			case "trash":
			case "untrash":
				if (!$_GET['out']) // inbox
					db_do_query(db_query_update(array('trashed'), array(($_POST['action'] == "trash") ? 1 : 0), "MAIL_TO", "mail_id IN ({$_POST['target']})"));	
				else // outbox
					db_do_query(db_query_update(array('sent', 'trashed'), array('sent', ($_POST['action'] == "trash") ? 1 : 0), "MAIL_HEADER", "mail_id IN ({$_POST['target']})"));
				break;
			case "markread":
			case "markunread":
				if (!$_GET['out']) // inbox
					db_do_query(db_query_update(array('hasread'), array(($_POST['action'] == "markread") ? 1 : 0), "MAIL_TO", "mail_id IN ({$_POST['target']})"));	
				else // outbox
					db_do_query(db_query_update(array('sent', 'hasread'), array('sent', ($_POST['action'] == "markread") ? 1 : 0), "MAIL_HEADER", "mail_id IN ({$_POST['target']})"));
				break;
			default:
				break;
		}
	}
	
	// list mails
	if (!$_GET['out'] and !$_GET['trash']) // inbox
		$sql = db_query_select(array(array(), "MAIL_HEADER.sent, MAIL_HEADER.subject, MAIL_HEADER.sender AS corr_id, MAIL_TO.*, MEMBER.nick"), array("(MAIL_HEADER INNER JOIN MAIL_TO ON MAIL_HEADER.mail_id = MAIL_TO.mail_id) INNER JOIN MEMBER ON MAIL_HEADER.sender = MEMBER.user_id", "DUMMY"), "MAIL_TO.user_id = {$_SESSION['user_id']} AND !MAIL_TO.trashed AND !MAIL_TO.deleted", "sent ASC");
	else if (!$_GET['out'] and $_GET['trash']) // inbox > trash
		$sql = db_query_select(array(array(), "MAIL_HEADER.sent, MAIL_HEADER.subject, MAIL_HEADER.sender AS corr_id, MAIL_TO.*, MEMBER.nick"), array("(MAIL_HEADER INNER JOIN MAIL_TO ON MAIL_HEADER.mail_id = MAIL_TO.mail_id) INNER JOIN MEMBER ON MAIL_HEADER.sender = MEMBER.user_id", "DUMMY"), "MAIL_TO.user_id = {$_SESSION['user_id']} AND MAIL_TO.trashed AND !MAIL_TO.deleted", "sent ASC");
	else if ($_GET['out'] and !$_GET['trash']) // outbox
		$sql = db_query_select(array(array(), "MAIL_HEADER.*, MAIL_TO.user_id, MAIL_TO.cc, MEMBER.nick"), array("(MAIL_HEADER INNER JOIN MAIL_TO ON MAIL_HEADER.mail_id = MAIL_TO.mail_id) INNER JOIN MEMBER ON MAIL_TO.user_id = MEMBER.user_id", "DUMMY"), "MAIL_HEADER.sender = {$_SESSION['user_id']} AND !MAIL_HEADER.trashed AND !MAIL_HEADER.deleted", "sent ASC, cc ASC");
	else if ($_GET['out'] and $_GET['trash']) // outbox > trashed
		$sql = db_query_select(array(array(), "MAIL_HEADER.*, MAIL_TO.user_id, MAIL_TO.cc, MEMBER.nick"), array("(MAIL_HEADER INNER JOIN MAIL_TO ON MAIL_HEADER.mail_id = MAIL_TO.mail_id) INNER JOIN MEMBER ON MAIL_TO.user_id = MEMBER.user_id", "DUMMY"), "MAIL_HEADER.sender = {$_SESSION['user_id']} AND MAIL_HEADER.trashed AND !MAIL_HEADER.deleted", "sent ASC, cc ASC");
		
	$sqlresult = db_do_query($sql);
	
	$mails = array();
	while ($m = db_fetch_array($sqlresult)) {
		if ($m['mail_id'] != $prev_m['mail_id']) {
			array_unshift($mails, $prev_m);
			$m['new'] = (($m['sent'] > $_SESSION['member']['last_visit']) and !$m['hasread']) ? 1 : 0;
			$m['nick'] = array($m['nick']);
			$m['corr_id'] = array($m['corr_id']);
			$prev_m = $m;
		} else {
			$prev_m['nick'][] = $m['nick'];
			$prev_m['corr_id'][] = $m['corr_id'];	
		}
	}
	array_unshift($mails, $prev_m);
	
	$CONTENT['mails'] = $mails;
	
	// add actions
	if (db_num_rows($sqlresult) > 0) {
		add_action("mails-markall", "javascript", "", "mark_all();");
		add_action("mails-markread", "javascript", "", "selected_markread();");
		add_action("mails-markunread", "javascript", "", "selected_markunread();");
		if (!$_GET['trash'])
			add_action("mails-trash", "javascript", "", "selected_trash();");
		else
			add_action("mails-untrash", "javascript", "", "selected_untrash();");
		add_action("mails-delete", "javascript", "", "selected_delete();");
	}
	
	$PAGE['content'] = $CONTENT;

}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE;
	print_maillist($PAGE['content']['mails'], $_GET['out'], $_GET['trash']);	
}

?>
